//
//  LPZWYTwoTableViewWithView.m
//  ZhongWangYuanApp
//
//  Created by liuyingping on 2018/6/4.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LPZWYTwoTableViewWithView.h"
#import "LPTwoTableFirstTableViewCell.h"
@interface LPZWYTwoTableViewWithView ()<UITableViewDelegate,UITableViewDataSource>

/**    */
@property (strong, nonatomic) UITableView  *firstTableView;
/**    */
@property (strong, nonatomic) NSArray  *firstDataSouce;

/**    */
@property (strong, nonatomic) UITableView  *secondTableView;
/**    */
@property (strong, nonatomic) NSArray  *secondDataSouce;

/**    */
@property (strong, nonatomic) NSString  *fid;
/**    */
@property (assign, nonatomic) BOOL   isFirst;
/**    */
@property (strong, nonatomic) LPTwoTableFirstTableViewCell  *lastCell;

@property (nonatomic , strong) NSString * firstId;



@end
@implementation LPZWYTwoTableViewWithView

- (instancetype)initWithFrame:(CGRect)frame firstArray:(NSArray *)firstArray firstFirstArray:(NSArray *)firstFirstArray
{
    self = [super initWithFrame:frame];
    if (self) {
        self.isFirst = YES;
        self.firstId = @"";
        self.fid = @"0";
        self.backgroundColor = [UIColor zj_colorWithR:20 G:20 B:20 Al:0.3];
        self.firstDataSouce = firstArray;
        self.secondDataSouce = firstFirstArray;
       
        [self layoutUI];
    }
    return self;
}



#pragma mark - 界面
- (void)layoutUI{
    
    [self addSubview:self.firstTableView];
    [self addSubview:self.secondTableView];
    self.firstTableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    self.secondTableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    NSInteger selectedIndex = 0;
    NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
    [self.firstTableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}


#pragma mark - 私有方法
- (void)hideView{
    if (self.delegate) {
        [self.delegate LPZWYTwoTableViewHide];
    }
    [UIView animateWithDuration:1 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
         [self removeFromSuperview];
    }];
}

- (void)showView{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1;
    }];
}

#pragma mark - 监听事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self hideView];
}

#pragma mark - 协议代理

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.firstTableView) {
      return  self.firstDataSouce.count;
    }else{
        return self.secondDataSouce.count;
    }
    
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    if (tableView == self.firstTableView) {
        
        LPTwoTableFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[LPTwoTableFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
     
        if (self.isFirst) {
            if (indexPath.row == 0) {
                cell.backgroundColor = [UIColor zj_colorWithHexString:@"dbdbdb"];
                self.isFirst = false;
                self.lastCell = cell;
            }
           
        }
        cell.titleLabel.text = [self.firstDataSouce[indexPath.row] objectForKey:@"name"];
        return cell;
    }else{
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.backgroundColor = [UIColor themeGrayColor];
        cell.textLabel.text = [self.secondDataSouce[indexPath.row] objectForKey:@"name"];
          cell.textLabel.font = LYPFONT(13);
        cell.textLabel.textColor = [UIColor grayColor];
        
        return cell;
    }
   
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.firstTableView) {
        self.firstId = [self.firstDataSouce[indexPath.row] objectForKey:@"fid"];
       self.fid = [self.firstDataSouce[indexPath.row] objectForKey:@"id"];
        [self getTwoData];
        self.lastCell.backgroundColor = [UIColor whiteColor];
        LPTwoTableFirstTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.backgroundColor = [UIColor zj_colorWithHexString:@"dbdbdb"];
        self.lastCell = cell;
    }else{
        if (self.delegate) {
            [self.delegate LPZWYTwoTableViewWithView_click_firstId:self.firstId secondFid:self.fid  threeFid:[self.secondDataSouce[indexPath.row] objectForKey:@"id"]];
        }
        [self hideView];
       
    }
}

- (void)getTwoData{
    [LPRequestManager request_mystudyallcoursecategoryWithfid:self.fid successBlock:^(id object) {
        NSString *code = [object objectForKey:@"code"];
        if ([code integerValue] == 1) {
            self.secondDataSouce = [object objectForKey:@"data"];
            [self.secondTableView reloadData];
        }
    } failBlock:^(id object) {
        
    }];
}




#pragma mark - getter
- (UITableView *)firstTableView{
    if (!_firstTableView) {
        _firstTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith] * 0.55, [UIScreen zj_screenHeight] * 0.5)];
        _firstTableView.delegate = self;
        _firstTableView.dataSource = self;
        _firstTableView.tableFooterView = [UIView new];
    }
    return _firstTableView;
}

- (UITableView *)secondTableView{
    if (!_secondTableView) {
        _secondTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.firstTableView.width, 0, [UIScreen zj_screenWith] * 0.45, [UIScreen zj_screenHeight] * 0.5)];
        _secondTableView.delegate = self;
        _secondTableView.dataSource = self;
        _secondTableView.backgroundColor = [UIColor themeGrayColor];
        _secondTableView.tableFooterView = [UIView new];
    }
    return _secondTableView;
}


@end
